/**
 * 
 */
package controller.xml;

import model.solver.ImportESolver;
import model.solver.ImportSolverParameter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link ImportESolver} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class ImportESolverXMLHandler extends XMLHandler {

	private ImportESolver			solver		= null;
	private ImportESolverXMLStatus	status		= ImportESolverXMLStatus.NONE;
	private ImportSolverParameter	param		= null;
	private String					description	= null;
	private String					execution	= null;
	private String					xmlvalue	= null;


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case PARAMETER:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case PARAMETER:
			if (qName.equals(XMLBaliseFormat.PARAMETER))
			{// ferme parameter
				status = ImportESolverXMLStatus.NONE;
				param = ((ImportedSolverParemeterXMLHandler) delegate).getParam();
				if (param == null)
					throw new SAXException("problem parameter");

				delegate = null;
				return true;
			}
			throw new SAXException("end of PARAMETER was expected");

		case DESCRIPTION:
			if (qName.equals(XMLBaliseFormat.IMPORTESOLVER_DESCRIPTION))
			{// ferme DESCRIPTION
				setDescription();
				status = ImportESolverXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of DESCRIPTION was expected");

		case EXEC:
			if (qName.equals(XMLBaliseFormat.IMPORTESOLVER_EXEC))
			{// ferme EXEC
				setExecution();
				status = ImportESolverXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of EXEC was expected");

		case XMLVALUE:
			if (qName.equals(XMLBaliseFormat.IMPORTESOLVER_XMLVALUE))
			{// ferme XMLVALUE
				setXMLValue();
				status = ImportESolverXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of XMLVALUE was expected");

		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}


	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != ImportESolverXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.PARAMETER))
		{// balise parameter
			if (arg3.getLength() != 1)
				throw new SAXException("problem attribute for PARAMETER");
			String at = arg3.getValue(XMLBaliseFormat.TYPE_PARAMETER);
			if (!at.equals(XMLBaliseFormat.TYPE_PARAMETER_SOLVEUR))
				throw new SAXException("bad attribute for PARAMETER");

			if (param != null)
				throw new SAXException("already have a PARAMETER");

			// set les donne pour recupere le parameter
			status = ImportESolverXMLStatus.PARAMETER;
			delegate = new ImportedSolverParemeterXMLHandler();
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.IMPORTESOLVER_DESCRIPTION))
		{// balise description
			if (description != null)
				throw new SAXException("already have a DESCRIPTION");

			status = ImportESolverXMLStatus.DESCRIPTION;
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.IMPORTESOLVER_EXEC))
		{// balise exec
			if (execution != null)
				throw new SAXException("already have a EXECUTION");

			status = ImportESolverXMLStatus.EXEC;
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.IMPORTESOLVER_XMLVALUE))
		{// balise xmlvalue
			if (xmlvalue != null)
				throw new SAXException("already have a XMLVALUE");

			status = ImportESolverXMLStatus.XMLVALUE;
			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the solver.
	 * 
	 * @return the solver
	 */
	public ImportESolver getSolver() {

		if (solver == null)
		{// pas encore creer
			if (execution == null || description == null || xmlvalue == null || param == null)
				return null;

			solver = new ImportESolver(execution, description, xmlvalue, param);
		}
		return solver;
	}

	/**
	 * Set the xmlvalue.
	 * 
	 * @throws SAXException
	 */
	private void setXMLValue() {
		String tmp = buffer.toString();
		if (tmp.isEmpty())
			new SAXException("xmlvalue is empty");

		xmlvalue = tmp;
	}

	/**
	 * Set the execution.
	 * 
	 * @throws SAXException
	 */
	private void setExecution() {
		String tmp = buffer.toString();
		if (tmp.isEmpty())
			new SAXException("execution is empty");

		execution = tmp;
	}

	/**
	 * Set the description.
	 */
	private void setDescription() {
		description = buffer.toString();
	}
}
